package com.bym.mall.controller;

import com.bym.mall.form.ShippingForm;
import com.bym.mall.service.IShippingService;
import com.bym.mall.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 收货地址模块
 * </p>
 *
 * @author bym
 * @since 2022-03-28
 */
@Api("收货地址模块")
@RequestMapping("/mall/shipping")
@RestController
public class ShippingController {

    @Autowired
    private IShippingService shippingService;

    /**
     * 添加地址
     *
     * @param shippingForm 地址表单
     * @return {@link ResponseVo}
     */
    @ApiOperation("添加地址")
    @PostMapping("/")
    public ResponseVo add(@RequestBody @Valid ShippingForm shippingForm) {
        return shippingService.add(shippingForm);
    }

    /**
     * 删除地址
     *
     * @param shippingId 地址id
     * @return {@link ResponseVo}
     */
    @ApiOperation("删除地址")
    @DeleteMapping("/{shippingId}")
    public ResponseVo delete(@PathVariable("shippingId") Integer shippingId) {
        return shippingService.delete(shippingId);
    }

    /**
     * 更新地址
     *
     * @param shippingId   地址id
     * @param shippingForm 地址表单
     * @return {@link ResponseVo}
     */
    @ApiOperation("更新地址")
    @PutMapping("/{shippingId}")
    public ResponseVo update(@PathVariable("shippingId") Integer shippingId,
                             @RequestBody @Valid ShippingForm shippingForm) {
        return shippingService.update(shippingId, shippingForm);
    }

    /**
     * 展示地址列表
     *
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @return {@link ResponseVo}
     */
    @ApiOperation("展示地址列表")
    @GetMapping("/")
    public ResponseVo list(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        return shippingService.list(pageNum, pageSize);
    }
}
